package com.ruoyi.project.order.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.page.TableSupport;
import com.ruoyi.project.order.domain.Customer;
import com.ruoyi.project.order.service.ICustomerService;
import com.ruoyi.project.order.vo.OrderListVo;
import com.ruoyi.project.order.vo.SelectOrderVo;
import com.ruoyi.project.system.role.domain.Role;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author L
 * @since 2020-08-07
 */
@Controller
@RequestMapping("/customer")
@Slf4j
public class CustomerController extends BaseController {
    @Autowired
    ICustomerService iCustomerService;

    private String prefix = "order/customer";

    @GetMapping()
    public String role() {
        return prefix + "/customer";
    }

    /**
     * 新增客户
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存客户
     */
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Customer customer) {
        //查询客户是否存在
        Customer rCustomer = iCustomerService.getOne(new QueryWrapper<Customer>().lambda()
                .eq(Customer::getCustomerName, customer.getCustomerName().trim())
                .eq(Customer::getCustomerPhone, customer.getCustomerPhone().trim())
                .eq(Customer::getCustomerAddress, customer.getCustomerAddress().trim())
        );
        if (rCustomer == null) {
            rCustomer = customer;
            log.info("添加客户: " + rCustomer);
        } else {
            return error("修改失败,用户已存在");
        }
        return toAjax(iCustomerService.save(rCustomer));

    }


    /**
     * 修改客户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer Id, ModelMap mmap) {
        mmap.put("customer", iCustomerService.getById(Id));
        return prefix + "/edit";
    }

    /**
     * 修改保存客户
     */
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Customer customer) {
        log.info("添加客户: " + customer);
        return toAjax(iCustomerService.updateById(customer));
    }


    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(Customer customer) {
        startPage();
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        Map<String, Object> map = BeanUtil.beanToMap(customer);
        for (String key : map.keySet()) {
            if (map.get(key) != null) {
                customerQueryWrapper.like(key, map.get(key));
            }
        }
        List<Customer> list = iCustomerService.list(customerQueryWrapper);
        return getDataTable(list);
    }


    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            for (String id : ids.split(",")) {
                iCustomerService.removeById(Integer.valueOf(id));
            }
            return toAjax(true);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @Log(title = "工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Customer customer) {
        log.info("导出");
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        Map<String, Object> map = BeanUtil.beanToMap(customer);
        for (String key : map.keySet()) {
            if (map.get(key) != null) {
                customerQueryWrapper.like(key, map.get(key));
            }
        }
        List<Customer> list = iCustomerService.list(customerQueryWrapper);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        return util.exportExcel(list, "工单数据");
    }


}
